/**
 * @(#)TransactionPanel.java
 *
 *
 * @author 
 * @version 1.00 2010/2/20
 */
package drops.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import drops.control.DentalTransactionControl;
import drops.dao.DentalTransaction;
import drops.dao.DentalTransactionPeer;

public class TransactionPanel extends JPanel implements ActionListener, TableModelListener, MouseListener {
	MainFrame mainFrame;

	JPanel headerPanel;
	
	Vector transactionHeader;
	Vector transactionData;
	
	List <DentalTransaction> transactionList;
	DefaultTableModel tableModel;
	JTable transactionTable;
	JLabel searchLabel;
	JButton addTransactionButton, deleteTransactionButton;

	SearchTransactionPanel searchTransactionPanel;
	
    public TransactionPanel(MainFrame mainFrame) {
    	this.mainFrame = mainFrame;
		setLayout(new BorderLayout());
		
		headerPanel = new JPanel();
		addTransactionButton = new JButton("Add Transaction");
		addTransactionButton.addActionListener(this);
		
		deleteTransactionButton = new JButton("Delete Transaction");
		deleteTransactionButton.addActionListener(this);
   		
		
		searchTransactionPanel = new SearchTransactionPanel(mainFrame);
		
		headerPanel.add(addTransactionButton);
		headerPanel.add(deleteTransactionButton);
		headerPanel.add(searchTransactionPanel);
		
		transactionList = new LinkedList <DentalTransaction>();
		
		transactionHeader = new Vector();
    	transactionData = new Vector(); 
		
    	
    	Vector rowData = new Vector();   
    	    	
		transactionHeader.add("Trans #");        
        transactionHeader.add("Pat #");
    	transactionHeader.add("Date");
    	transactionHeader.add("Desc");
        transactionHeader.add("Time"); 
 		transactionHeader.add("Debit");
 		transactionHeader.add("Credit Date"); 
 		transactionHeader.add("Credit Amount");
 		transactionHeader.add("Balance");
 		
 		tableModel = new DefaultTableModel(transactionData,transactionHeader) {
 			public boolean isCellEditable(int row, int col) {
 				return false;
 			}
 		};
 		tableModel.addTableModelListener(this);
    	transactionTable = new JTable(tableModel);
    	transactionTable.setAutoCreateRowSorter(true);
    	transactionTable.setFillsViewportHeight(false);
    	transactionTable.addMouseListener(this);
    	JScrollPane scrollPane = new JScrollPane(transactionTable);
		
    	transactionTable.setRowHeight(20);
		transactionTable.getTableHeader().setReorderingAllowed(false);
    	add(headerPanel, BorderLayout.NORTH);
    	add(scrollPane, BorderLayout.CENTER);
    }
    
    private void updateModel() {
    	transactionData = new Vector();
    	
    	for (int i=0, size=transactionList.size();i<size;i++) {
    		DentalTransaction t = transactionList.get(i);
    		Vector rowData = new Vector();
    		DateFormat df = DateFormat.getDateInstance();
    		DateFormat tf = DateFormat.getTimeInstance();
    		rowData.add(t.getTransactionNumber()+"");
    		rowData.add(t.getPatientNumber()+"");
    		rowData.add(df.format(t.getDate()));
    		rowData.add(t.getDescription());
    		rowData.add(tf.format(t.getDate()));
    		rowData.add(t.getDebit()+"");
    		rowData.add(df.format(t.getCreditDate()));
    		rowData.add(t.getCreditAmount()+"");
    		rowData.add(t.getBalance()+"");
    		transactionData.add(rowData);
    	}
    	tableModel.setDataVector(transactionData, transactionHeader);
    }

	public void search(String input, String category) {
		transactionList = DentalTransactionControl.searchTransaction(input,category);
		updateModel();
	}
	
	public void search(Date date, String category) {
		transactionList = DentalTransactionControl.searchTransactionByDate(date);
		updateModel();
	}
	
	public void delete() {
		int i = transactionTable.getSelectedRow();
		if (i>=0) {
			int transactionNumber = Integer.parseInt(tableModel.getValueAt(i, 0).toString());
			DentalTransactionControl.deleteDentalTransaction(transactionNumber);
			tableModel.removeRow(i);
		}
		else {
			JOptionPane.showMessageDialog(this, "No row selected.");
		}
	}

	public void search() {
		searchTransactionPanel.search(true);
	}
	
	public void refresh() {
		searchTransactionPanel.search(false);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==addTransactionButton) {
			addTransaction();
		}
		else if (e.getSource()==deleteTransactionButton) {
			delete();
		}
	}

	private void addTransaction() {
		mainFrame.addTransactionFrame();
	}

	public void update(int row) {
		
	}
	
	@Override
	public void tableChanged(TableModelEvent e) {
		if (e.getType()==TableModelEvent.UPDATE) {
			update(e.getFirstRow());
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getSource()==transactionTable) {
			if (arg0.getClickCount()==2) {
				mainFrame.editTransactionFrame();
				mainFrame.setTransaction(Integer.parseInt(tableModel.getValueAt(transactionTable.getSelectedRow(), 0).toString()));
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
    
}